﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Controls;
using Petroleum.UC.Public;
using Petroleum.Model;
using Petroleum.DB.Model;
using HZH_Controls.Forms;
using Newtonsoft.Json;
using Petroleum.Tool;
using System.Data.SQLite;

namespace Petroleum.UC
{/// <summary>
/// 地层压力测试
/// </summary>
    public partial class Step2FormationPressurePrediction : UserControl
    {
        FormMain main = new FormMain();
        bool hassave = false;
        public List<Pressure> list = new List<Pressure>();
        public List<Image> images = new List<Image>();
        /// <summary>
        /// 当前Elist列表
        /// </summary>
        List<Elist> elists = new List<Elist>();
        public Pressure CurrentPressure = new Pressure();

        Pressure pressure = new Pressure();//新增
        void InitControl()
        {
            Age.Source = DropDownModel.AGElstCom;
            Age.SelectedIndex = 1;
            GeologicalDivisions.Source = DropDownModel.GeologicalstCom;
            GeologicalDivisions.SelectedIndex = 1;
            //GeologicalDivisions.Source = DropDownModel.GeologicalstCom;
            //GeologicalDivisions.SelectedIndex = 1;

            images.Add(Properties.Resources.CS1);
            images.Add(Properties.Resources.CS2);
            images.Add(Properties.Resources.CS3);
            images.Add(Properties.Resources.CS4);
            images.Add(Properties.Resources.CS5);

            ColumnarSection.DrawMode = DrawMode.OwnerDrawFixed;
            ColumnarSection.DropDownStyle = ComboBoxStyle.DropDownList;
            ColumnarSection.DrawItem += comboBox1_DrawItem;

            ColumnarSection.Items.Add("1");
            ColumnarSection.Items.Add("2");
            ColumnarSection.Items.Add("3");
            ColumnarSection.Items.Add("4");
            ColumnarSection.Items.Add("5");
            ColumnarSection.SelectedIndex = 0;
            txtsstvd.TextChanged += Txtsstvd_TextChanged;
        }
        private void Txtsstvd_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtsstvd.InputText) && txtsstvd.InputText != "." && (txtsstvd.InputText.Length > 1 && txtsstvd.InputText.StartsWith("-")))
            {
                lbltvd.InputText = (0 - decimal.Parse(txtsstvd.InputText) + Session.BasicInf.KB) + "";
            }
        }
        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.Graphics.DrawImage(images[e.Index], 0, e.Bounds.Y, 16, 16);
            e.Graphics.DrawString(ColumnarSection.Items[e.Index].ToString(), new Font("宋体", 12), new SolidBrush(Color.Red), 20, e.Bounds.Y + 1);
        }
        public Step2FormationPressurePrediction(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            InitControl();
        }
        private void dgv_Load(object sender, EventArgs e)
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ordernum", HeadText = "序号", Width = 100, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "AGE", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "GeologicalDivisions", HeadText = "Geological \n Divisions", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ColumnarSection", HeadText = "Columnar \n Section", Width = 150, WidthType = SizeType.Absolute, CustomCellType = typeof(Step2_CPC) });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "SSTVD", HeadText = "SSTVD", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "TVD", HeadText = "TVD", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Formation_Coe", HeadText = "Formation\nPressure\nCoefficient", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Collapse_Coe", HeadText = "Collapse\nPressure\nCoefficient", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "FormationFrac_Coe", HeadText = "Formation\nFracture\nPressure\nCoefficient", Width = 120, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Lithology", HeadText = "Lithology", Width = 220, WidthType = SizeType.Absolute });
            dgv.RowHeight = 80;
            this.dgv.Columns = lstCulumns;
            this.dgv.DataSource = list.OrderBy(n => n.ordernum).ToList();
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step1(main));
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            hassave = true;
            main.AddControl(new Step2Img(main));
        }
        private void btnAdd_BtnClick(object sender, EventArgs e)
        {
            pressure.SSTVD = decimal.Parse(txtsstvd.InputText);
            pressure.TVD = decimal.Parse(lbltvd.InputText);
            pressure.Age = Age.SelectedValue;
            pressure.GeologicalDivisions = GeologicalDivisions.SelectedValue;
            pressure.ColumnarSection = ColumnarSection.SelectedItem.ToString();
            pressure.Formation_Coe = txtFormation_pre.InputText;
            pressure.Collapse_Coe = txtCollapse.InputText;
            pressure.FormationFrac_Coe = txtfracture.InputText;
            pressure.Lithology = txtlithlogy.InputText;
            pressure.ordernum = int.Parse(txtorder.InputText);
            if (CurrentPressure != null && !string.IsNullOrEmpty(CurrentPressure.Age))
                list.Remove(CurrentPressure);
            list.Add(pressure);
            this.dgv.DataSource = list.OrderBy(n => n.ordernum).ToList();
            pressure = new Pressure();
        }

        void SaveData()
        {
            string imgc = "";
            foreach (var item in list)
            {
                switch (item.ColumnarSection)
                {
                    case "1":
                        imgc = SpireDocUtil.ImagesPath + "Images\\CS1.png";
                        break;
                    case "2":
                        imgc = SpireDocUtil.ImagesPath + "Images\\CS2.png";
                        break;
                    case "3":
                        imgc = SpireDocUtil.ImagesPath + "Images\\CS3.png";
                        break;
                    case "4":
                        imgc = SpireDocUtil.ImagesPath + "Images\\CS4.png";
                        break;
                    case "5":
                        imgc = SpireDocUtil.ImagesPath + "Images\\CS5.png";
                        break;
                }
                item.ImgColumnarSection = imgc;
            }
            Session.FormationPressure.list = list;
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var FormationPressure = main.projects.Where(n => n.TableName == TableNames.FormationPressure.ToString()).FirstOrDefault();
            if (FormationPressure != null)
            {
                Session.FormationPressure = JsonConvert.DeserializeObject<FormationPressure>(FormationPressure.TableData);
                list = Session.FormationPressure.list;
                if (list != null)
                    this.dgv.DataSource = list.OrderBy(n => n.ordernum).ToList();
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            if (!hassave) SaveData();
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.FormationPressure.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.FormationPressure.ToString(), JsonConvert.SerializeObject(Session.FormationPressure));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.FormationPressure.ToString(), JsonConvert.SerializeObject(Session.FormationPressure), 1);

            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                list.RemoveAt(row);
                this.dgv.DataSource = list.OrderBy(n => n.ordernum).ToList();
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        private void StripMenuEdit_Click(object sender, EventArgs e)
        {
            CurrentPressure = list[this.dgv.SelectRow.RowIndex];
            if (CurrentPressure != null)
            {
                txtsstvd.InputText = CurrentPressure.SSTVD + "";
                lbltvd.InputText = CurrentPressure.TVD + "";
                Age.SelectedValue = CurrentPressure.Age;
                GeologicalDivisions.SelectedValue = CurrentPressure.GeologicalDivisions;
                ColumnarSection.SelectedValue = CurrentPressure.ColumnarSection;
                txtFormation_pre.InputText = CurrentPressure.Formation_Coe;
                txtCollapse.InputText = CurrentPressure.Collapse_Coe;
                txtfracture.InputText = CurrentPressure.FormationFrac_Coe;
                txtlithlogy.InputText = CurrentPressure.Lithology;
                txtorder.InputText = CurrentPressure.ordernum + "";
                //this.elists = CurrentPressure.elist;
            }
        }
        private void btnmore_BtnClick(object sender, EventArgs e)
        {
            GridToolTable grid = new GridToolTable("添加更多", CallBackData, CurrentPressure.elist);
            grid.ShowDialog();
            grid.Dispose();
        }
        void CallBackData(List<Elist> eli)
        {
            if (eli != null)
            {
                pressure.elist = eli;
            }
        }
    }
}
